#include "kzf_config.h"

kzf_config::kzf_config() {

}


bool kzf_config::readJsonFileToRootObject(QString path)
{
    QFile file(path);
    if (!file.open(QFile::ReadOnly | QFile::Text)) {
        qDebug() << "can't open error!";
        return false;
    }

    // 读取文件的全部内容
    QTextStream stream(&file);
    stream.setCodec("UTF-8");		// 设置读取编码是UTF8

    QString str = stream.readAll();
    // qDebug()<<str;

    QJsonParseError jsonError;
    QJsonDocument doc = QJsonDocument::fromJson(str.toUtf8(), &jsonError);
    if (jsonError.error != QJsonParseError::NoError && !doc.isNull()) {
        qDebug() << "Json格式错误！" << jsonError.error;
        return false;
    }

    RconfigJson = doc.object();
    qDebug()<<"read json data:"<<RconfigJson;

    file.close();
    return true;
}

bool kzf_config::analysisJsonData()
{
    QStringList rootList = RconfigJson.keys();
    if(rootList.length()==0){
        return false;
    }
    // qDebug()<<"config-rootList"<<rootList;

    if(rootList.contains("config")){
        QJsonObject configObj = RconfigJson.value("config").toObject();
        if(configObj.contains("user")){
            user = configObj.value("user").toString();
        }
        if(configObj.contains("version")){
            version = configObj.value("version").toString();
        }
    }

    if(rootList.contains("camera")){
        QJsonObject configObj = RconfigJson.value("camera").toObject();

        if(configObj.contains("cameraName")){
            cameraName = configObj.value("cameraName").toString();
        }
        if(configObj.contains("cameraType")){
            cameraType = configObj.value("cameraType").toString();
        }
        if(configObj.contains("cameraWidth")){
            cameraWidth = configObj.value("cameraWidth").toInt();
        }
        if(configObj.contains("cameraHight")){
            cameraHight = configObj.value("cameraHight").toInt();
        }
        if(configObj.contains("umPerPixel")){
            umPerPixel = configObj.value("umPerPixel").toDouble();
        }
        if(configObj.contains("pixelSize")){
            pixelSize = configObj.value("pixelSize").toDouble();
        }

    }
    // qDebug()<<user<<version;
    // qDebug()<<cameraWidth<<cameraHight<<umPerPixel<<pixelSize<<cameraName<<cameraType;
    return true;
}
bool kzf_config::dataToJson()
{
    // QJsonObject root;
    QJsonObject config;
    QJsonObject camera;

    config.insert("user", user);
    config.insert("version", version);

    camera.insert("cameraName", cameraName);
    camera.insert("cameraType", cameraType);
    camera.insert("cameraWidth", cameraWidth);
    camera.insert("cameraHight", cameraHight);
    camera.insert("umPerPixel", umPerPixel);
    camera.insert("pixelSize", pixelSize);


    WconfigJson.empty();
    WconfigJson.insert("config", config);
    WconfigJson.insert("camera", camera);

    // qDebug()<<camera<<config<<WconfigJson;

    return true;
}

bool kzf_config::writeRootObjectToJsonFile(QString path)
{

    QJsonDocument document;
    document.setObject(WconfigJson);

    QFile writeFile(path);
    if (!writeFile.open(QFile::WriteOnly | QFile::Truncate)) {
        qDebug() << "can't open error!";
        return false;
    }

    QTextStream wirteStream(&writeFile);
    wirteStream.setCodec("UTF-8");		// 设置读取编码是UTF8
    wirteStream << document.toJson();		// 写入文件
    writeFile.close();					// 关闭文件

    return true;
}

